package ai.people.netmon.msgsearch.domain.response;

import ai.people.netmon.framework.domain.searchmsg.bean.AuthorInfo;
import ai.people.netmon.framework.domain.searchmsg.bean.GroupInfo;
import ai.people.netmon.utils.StringPool;
import ai.people.netmon.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 信息数据结果
 *
 * @author yuanqinglong
 * @description:
 * @author: Xing.Wei
 * @time: 2021-05-11 14:26
 * @date 2022/08/15
 */
@Data
public class InfoDataResult implements Serializable {
    private String id; // ID
    private String ps;
    private String pubtime; // 发布时间
    private String sitename;
    private String source; // 来源
    private String title; // 标题
    private String url; // 链接
    private String content; // 内容
    private String domain; // 域名
    private GroupInfo group; // 群组
    private AuthorInfo author; // 账号
    private String tags;// 标签
    private String sentiment;


    /**
     * 构建
     *
     * @param esData 西文数据
     * @return {@link InfoDataResult}
     */
    public static InfoDataResult build(Map esData){
        InfoDataResult infoDataResult = JSON.parseObject(JSON.toJSONString(esData), InfoDataResult.class);
        String releaseTime = StringUtils.formatDate("yyyy-MM-dd HH:mm:ss", (String) esData.get("date"));
        String gatherTime = StringUtils.formatDate("yyyy-MM-dd HH:mm:ss", (String) esData.get("ts"));
        infoDataResult.setPs(gatherTime);
        infoDataResult.setPubtime(releaseTime);
        infoDataResult.setSource((String) esData.get("info_type"));
        infoDataResult.setAuthor(new AuthorInfo((String) esData.get("user_id"),(String) esData.get("user_name")));
        infoDataResult.setGroup(new GroupInfo((String) esData.get("group_id"),(String) esData.get("group_name")));
        Object tags = esData.get("tag");
        if (Objects.nonNull(tags)) {
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(tags));
            String tagsNames = jsonObject.getJSONArray("tag").stream().map(String::valueOf).collect(Collectors.joining(StringPool.COMMA));
            infoDataResult.setTags(tagsNames);
        }
        return infoDataResult;
    }
}
